<?php

namespace App\Controllers\Lessions;

use App\Models\Lession\Lession;
use App\Models\Tag\Tags;
use \App\Libs\Views\Json;
use App\Models\Lession\Scolumn;

/**
    专栏详情;
 */
class GetColumnDetail extends \App\Libs\Base\BaseAction
{

    public function execute()
    {
        if(!$this->request->hasQuery('column_id')){
            return \App\Libs\Views\Json::view(1, '缺少参数', null);
        }
        $column = $this->request->getQuery('column_id');
        $matchColumn = Scolumn::findOne([
            'conditions' => 'id=?1 and isshow=1',
            //'columns' => 'id,column_bango,title,column_detail,flower_num,comment_num',
            'bind' => [1=>$column]
        ]);
        if(empty($matchColumn)){
            return \App\Libs\Views\Json::view(1, '指定的数据不存在', null);
        }
        //关系找专栏的课程
        $lessions = $matchColumn->getLessions([
            'conditions' => 'type=2',   //这里的条件是补充，不是全部
            'columns' => 'id,serial,title,little_intro,comment_num,flower_num,comment_num'
        ]);
        //循环耗能大
        /*
        $allCommentNum = 0;
        $allFowerNum = 0;
        foreach($lessions as $val){
            $allCommentNum+=$val->comment_num;
            $allFowerNum+=$val->flower_num;
        }
        */
        $haveUpdate = count($lessions);
        $columnData = $matchColumn->toArray(['id','column_bango','title','column_detail','flower_num','comment_num']);
        /*
        $columnData['comment_num'] = $allCommentNum;
        $columnData['flower_num'] = $allFowerNum;
        */
        $columnData['haveUpdate'] = $haveUpdate;
        $columnData['type'] = 2;
        $columnData['type_text'] = 'scolumn';
        $columnData['lessions'] = $lessions;

        return \App\Libs\Views\Json::view(0, '成功', $columnData);
    }
}
